<?php

class bidding extends CI_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('biddingconfirm_model');
        $this->load->model('check_model');


        $this->load->model('bidding_model');
        $this->load->model('biddingcheck_model');
        $this->load->model('procurement_model');
        $this->load->model('supplier_model');
        $this->load->model('biddingwait_model', 'wait_model');
        $this->load->model('biddingwaitfile_model', 'waitFile_model');
        $this->load->model('user_model');
        $this->load->model('check_model');
        $this->load->model('project_model');
        $this->load->library('Qiniu');
    }

    //确认定标评审员
    public function confirmReviewers()
    {
        //修改定标数据库的状态信息
        $data = [
            'status' => 1,
            'isEnd' => 0,
        ];
        $where = [
            'id' => $_POST['id']
        ];
        $edit = $this->biddingconfirm_model->edit($data, $where);

        if($edit){
            $checkData = [
                'type' => 2,
                'relevanceId' => $_POST['relevanceId'],
                'checkinfo' => '',
                'checker' => $_POST['checkPeople'],
                'status' => 0,
                'checkStatus' => 0,
                'howLevelCheck' => 0,
                'createTime' => time(),
                'checkOrderType' => '招标项目定标'
            ];

            $this->check_model->add($checkData);
        }

        if ($edit) {
            $result = [
                'success' => 1
            ];
        } else {
            $result = [
                'success' => 1
            ];
        }

        echo json_encode($result);

    }



    //招投标项目管理列表筛选搜索
    public function biddingSearch(){
        $argc = [];
        if(isset($_GET['startTime'])  && $_GET['startTime']){
            $argc += [
                'createTime >'=> strtotime($_GET['startTime']),
            ];
        }

        if(isset($_GET['endTime'])  && $_GET['endTime']){
            $argc += [
                'createTime <'=> strtotime($_GET['endTime']),
            ];
        }


        if(isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null' ){
            $argc += [
                'status' => $_GET['status']
            ];
        }




        if(isset($_GET['search']) && $_GET['search']){
            $searchArg = [
                'field' => 'name',
                'arg'=> $_GET['search'],
                'side' => 'both'
            ];
        }else{
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        if(!$argc){
            $argc = 0;
        }



        if($searchArg){

            $data = $this->bidding_model->search('*',$argc,$searchArg,$page_num,$start_position);

        }else{
            $data = $this->bidding_model->page('bidding',$argc,$page_num,$start_position);
        }

        if($data[0]){
            foreach ($data[0] as $k => $v) {
                //判断如果关联的需求单存在,就去查询
                if($v['needCode']){
                    $arg = [
                        'needCode' => $v['needCode']
                    ];
                    $procurementData[] = $this->procurement_model->getByFieldQueryData('id,needCode', $arg, 0);

                }

                $arg2 = [
                    'isEnd' => 1,
                    'biddingId' => $v['id']
                ];

                //确认完成的数量
                $data[0][$k]['completeNum'] = $this->wait_model->getByFieldQueryData('id,status,isEnd', $arg2, 1);

                //根据供应商id去查询待办表 = 5的,说明已经上传了资料,

                $supplierId[] = json_decode($v['supplierInfo'],true);

                unset($data[0][$k]['synopsis']);

//                if($v['supplierNum'] = )
//
//                print_r($supplierNum);
//                die();
            }



            //判断是否结束
            foreach ($data[0] as $k2 => $v2) {
                if(isset($procurementData) && $procurementData){
                    foreach ($procurementData as $k3 => $v3) {
                        if ($v2['needCode'] == $v3['needCode']) {
                            $data[0][$k2]['needId'] = $v3['id'];
                        }
                    }
                }

                //如果供应商完成数量跟所选供应商一直,就更新招标单的状态为 已完成

                //如果时间到了到期时间也表示结束
                if ($v2['supplierNum'] > 0 || time() > $v2['validDownTime']) {

                    if ((count($v2['completeNum']) == $v2['supplierNum']) || time() > $v2['validDownTime']) {

                        $biddingData = [
                            'status' => 2,
                            'isEnd' => 1
                        ];

                        $biddingWhere = [
                            'id' => $v2['id']
                        ];

//                        __logInfo('debug',$v2['id']);

                        $this->bidding_model->edit($biddingData, $biddingWhere);

                        if (!$v2['isEnd']) {
                            //防止冲突提交
                            //已完成之后新增到项目审核表
                            $checkAdd = [
                                'biddingId' => $v2['id'],
                                'code' => $v2['code'], //项目编码
                                'name' => $v2['name'],       //项目名称
                                'createTime' => $v2['createTime'], //发布时间
                                'address' => $v2['address'], //所在地区
                                'needCode' => $v2['needCode'], //需求单编号
                                'validTime' => $v2['validTime'], //有效时间
                                'projectImg' => $v2['projectImg'], //项目附件

                                'supplierInfo' => $v2['supplierInfo'], //供应商信息id,传json字符串
                                'supplierNum' => count(json_decode($v2['supplierInfo'], true)), //供应商数量

                                'synopsis' => $v2['synopsis'], //项目简介

                                //其他信息
                                'needData' => $v2['needData'],  //需要的资质数据,传json字符串
                                'needAmount' => $v2['needAmount'], //需要缴纳的保证金
                                'contactMan' => $v2['contactMan'], //联系人
                                'phone' => $v2['phone'],  //联系电话
                                'biddingFile' => $v2['biddingFile'],  //上传标书文件
                                'procurementImg' => $v2['procurementImg'],  //上传采购清单
                                'status' => 0
                            ];

                            $this->biddingcheck_model->add($checkAdd);

                        }


                    }
                }


                $data[0][$k2]['validDownTime'] = __downTime($v2['validDownTime']);

                $data[0][$k2]['createTime'] = date('Y-m-d',$v2['createTime']);
            }
        }



//        echo '<pre>';
//        print_r($data);
//        die;


        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;


        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }


    //供应商待办列表筛选搜索
    public function waitingListSearch(){

        $argc = [];
        if(isset($_GET['startTime'])  && $_GET['startTime']){
            $argc += [
                'createTime >'=> strtotime($_GET['startTime']),
            ];
        }

        if(isset($_GET['endTime'])  && $_GET['endTime']){
            $argc += [
                'createTime <'=> strtotime($_GET['endTime']),
            ];
        }

        if(isset($_GET['status']) && $_GET['status'] != '' && $_GET['status'] != 'null'){
            $argc += [
                'status' => $_GET['status']
            ];
        }

        if(isset($_GET['search']) && $_GET['search'] != ''){
            $searchArg = [
                'field' => 'name',
                'arg'=> $_GET['search'],
                'side' => 'both'
            ];
        }else{
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        if(!$argc){
            $argc = [];
        }

        $argc += [
            'supplierInfo' => isset($_COOKIE['supplierId']) ? $_COOKIE['supplierId'] : 0
        ];

//        print_r($argc);
//        die;


        if($searchArg){

            $data = $this->wait_model->search('*',$argc,$searchArg,$page_num,$start_position);

        }else{
            $data = $this->wait_model->page('biddingwait',$argc,$page_num,$start_position);
        }

        if($data[0]){
            //如果已支付status=2,就根据biddingId去查询bidding表的id查询标书文件
            foreach ($data[0] as $k=>$v){
                if($v['status'] == 2){
                    $ids[] = $v['biddingId'];  //管理的标书文件id
                }
            }
//            echo '<pre>';
//            print_r($ids);
//            die;
            if(isset($ids)){
                foreach ($ids as $k2=>$v2){
                    $arg = [
                        'id' =>  $v2
                    ];

                    $biddingFile[] = $this->bidding_model->getByFieldQueryData('id,code,biddingFile',$arg,0);
                }
            }

//                    echo '<pre>';
//            print_r($biddingFile);
//            die;


            foreach ($data[0] as $k3=>$v3){
                if(isset($biddingFile)){
                    foreach ($biddingFile as $k4=>$v4){
                        if($v3['biddingId'] == $v4['id']){
                            $data[0][$k3]['biddingFile'] = $v4['biddingFile'];
                        }
                    }
                }

                $data[0][$k3]['createTime'] = date('Y-m-d',$v3['createTime']);
            }
        }





        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;


        echo json_encode($data,JSON_UNESCAPED_UNICODE);

    }


    //定标管理列表筛选搜索
    public function checkListSearch(){
        $argc = [];
        if(isset($_GET['startTime'])  && $_GET['startTime']){
            $argc += [
                'createTime >'=> strtotime($_GET['startTime']),
            ];
        }

        if(isset($_GET['endTime'])  && $_GET['endTime']){
            $argc += [
                'createTime <'=> strtotime($_GET['endTime']),
            ];
        }

        if(isset($_GET['status']) && $_GET['status'] !='' && $_GET['status'] != 'null'){
            $argc += [
                'status' => $_GET['status']
            ];
        }

        if(isset($_GET['search']) && $_GET['search']){
            $searchArg = [
                'field' => 'name',
                'arg'=> $_GET['search'],
                'side' => 'both'
            ];
        }else{
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        if(!$argc){
            $argc = 0;
        }

//        echo json_encode($argc);
//        die;



        if($searchArg){
            $data = $this->biddingcheck_model->search('*',$argc,$searchArg,$page_num,$start_position);

        }else{



            $data = $this->biddingcheck_model->page('biddingcheck',$argc,$page_num,$start_position);
        }

        if($data[0]){
            foreach ($data[0] as $k => $v) {
                $arg = [
                    'needCode' => $v['needCode']
                ];
                $procurementData[] = $this->procurement_model->getByFieldQueryData('id,needCode', $arg, 0);

                //查询有效的供应商信息
                //根据当前标书id和供应商id去查询文件表是否存在,如果存在就说明有
                $supplierIds[$v['biddingId']] = json_decode($v['supplierInfo'], true);

                unset($data[0][$k]['synopsis']);
            }


            if(isset($supplierIds) && $supplierIds){
                foreach ($supplierIds as $k2=>$v2){
                    //根据供应商id查询供应商文件表是否有数据,有数据就显示,没数据就不显示
                    foreach ($v2 as $k3=>$v3){
                        $arg3 = [
                            'supplierInfo' => $v3,
                            'biddingWaitId' => $k2
                        ];

                        $supplierFile[] = $this->waitFile_model->getByFieldQueryData('id,biddingWaitId', $arg3, 0);
                    }
                }
            }



            foreach ($data[0] as $k4=>$v4){
                if(isset($supplierFile) && $supplierFile) {
                    foreach ($supplierFile as $k5 => $v5) {
                        if ($v4['biddingId'] == $v5['biddingWaitId']) {
                            $data[0][$k4]['validSupplier'][] = $v5['id'];  //文件表的id
                        }
                    }
                }
            }


            foreach ($data[0] as $k6 => $v6) {
                if(isset($procurementData)){
                    foreach ($procurementData as $k7 => $v7) {
                        if ($v6['needCode'] == $v7['needCode']) {
                            $data[0][$k6]['needId'] = $v7['id'];
                            break;
                        }else{
                            $data[0][$k6]['needId'] = '';
                        }
                    }
                }


                //插叙已审核人员的信息
                //统计数量,如果相同就修改状态为已完成
                //如果评分在count等于评委人数 就修改状态为评审完成


                //有效供应商数量 * 选择的评审人 == checkinfo数量
                //2 * 4 = 8  c
                //如果已经接触了,就不加入到定标数据库中了



                if (!$v6['isEnd']) {
                    $countPeople = count(json_decode($v6['checkPeople'],true));  //审核人员数
                    $countCheckInfo = count(json_decode($v6['checkInfo'],true) );  //审核人员信息
                    if(isset($v6['validSupplier']) && $v6['validSupplier']){
                        $countValidSupplier = count($v6['validSupplier']);  //有效的供应商数量
                    }else{
                        $countValidSupplier = 0; //判断变量是否存在，不存在就为0
                    }

                    __logInfo('checkPeople',$countPeople);
                    __logInfo('checkInfo',$countCheckInfo);

                    __logInfo('validSupplierNum',$countValidSupplier);

                    if ($countPeople > 0 || $countValidSupplier > 0) {
                        $num = $countPeople * $countValidSupplier;



                        // checkInfo 确定完成
//                        __logInfo('num',$num);
//                        __logInfo('asdasd',)
                        if($num > 0 && $v6['checkInfo']){
                            __logInfo('$num',$num);
                            __logInfo('$countCheckInfo',$countCheckInfo);

                            if ($num  == $countCheckInfo) {

                                $editData = [
                                    'status' => 2,
                                    'isEnd' => 1
                                ];
                                $editWhere = [
                                    'id' => $v6['id']
                                ];

//                            __logInfo('a',$editWhere);
                                $this->biddingcheck_model->edit($editData, $editWhere);


                                //新增到项目定标列表
                                $confirmData = [
                                    'code' => $v6['code'],
                                    'name' => $v6['name'],
                                    'createTime' => $v6['createTime'],
                                    'needAmount' => $v6['needAmount'],
                                    'address' => $v6['address'],
                                    'relevanceId' => $v6['id'],  //已审批完成的数据id , 标书id
                                    'status' => 0,
                                    'isEnd' => 0,
                                ];


                                $this->biddingconfirm_model->add($confirmData);
                            }
                        }

                    }


                }


                $data[0][$k6]['createTime'] = date('Y-m-d',$v6['createTime']);


            }
        }







        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;


        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    //招投标定标列表筛选搜索
    public function confirmListSearch(){
        $argc = [];
        if(isset($_GET['startTime'])  && $_GET['startTime']){
            $argc += [
                'createTime >'=> strtotime($_GET['startTime']),
            ];
        }

        if(isset($_GET['endTime'])  && $_GET['endTime']){
            $argc += [
                'createTime <'=> strtotime($_GET['endTime']),
            ];
        }

        if(isset($_GET['status']) && $_GET['status'] !='' && $_GET['status'] != 'null'){
            $argc += [
                'status' => $_GET['status']
            ];
        }

        if(isset($_GET['search']) && $_GET['search'] !=''){
            $searchArg = [
                'field' => 'name',
                'arg'=> $_GET['search'],
                'side' => 'both'
            ];
        }else{
            $searchArg = '';
        }


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        if(!$argc){
            $argc = 0;
        }



        if($searchArg){

            $data = $this->biddingconfirm_model->search('*',$argc,$searchArg,$page_num,$start_position);

        }else{
            $data = $this->biddingconfirm_model->page('biddingconfirm',$argc,$page_num,$start_position);
        }

        if($data[0]){
            foreach ($data[0] as $k => $v) {

                $arg = [
                    'id' => $v['relevanceId'],
                    'isEnd' => 1
                ];

                $field = 'id,code,needCode,name,createTime,validTime,address,contactMan';
                $biddinfCheckInfo[] = $this->biddingcheck_model->getByFieldQueryData($field, $arg, 0);

            }

            // echo '<pre>';
            // print_r($biddinfCheckInfo);
            // die();

            foreach ($biddinfCheckInfo as $k2 => $v2) {
                if ($v2['needCode']) {
                    $arg = [
                        'needCode' => $v2['needCode']
                    ];
                    $procurementData[] = $this->procurement_model->getByFieldQueryData('id,needCode', $arg, 0);
                }


                foreach ($data[0] as $k3 => $v3) {
                    if ($v3['relevanceId'] == $v2['id']) {
                        $data[0][$k2]['code'] = $v2['code'];
                        $data[0][$k2]['needCode'] = $v2['needCode'];
                        $data[0][$k2]['name'] = $v2['name'];
                        $data[0][$k2]['createTime'] = $v2['createTime'];
                        $data[0][$k2]['validTime'] = $v2['validTime'];
                        $data[0][$k2]['address'] = $v2['address'];
                        $data[0][$k2]['contactMan'] = $v2['contactMan'];
                    }
                }
            }


            foreach ($data[0] as $k4 => $v4) {
                if (isset($procurementData)) {
                    foreach ($procurementData as $k5 => $v5) {
                        if ($v4['needCode'] == $v5['needCode']) {
                            $data[0][$k4]['needId'] = $v5['id'];
                        }
                    }
                } else {
                    $data[0][$k4]['needId'] = '';

                }

                $data[0][$k4]['createTime'] = date('Y-m-d',$v4['createTime']);

            }


        }


        $data['pageNum'] = $page_num;
        $data['this_page'] = $page;


        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }
}










































